package com.youlu.campus.service.university.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.UniversityInfo;
import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.UserParticipateActivityInfo;
import com.youlu.campus.service.utils.IkAnalyzerUtil;
import com.youlu.campus.service.university.UniversityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@Slf4j
@Service
public class UniversityServiceImpl implements UniversityService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public UniversityInfo get(UserPO userPO) {
        log.info(":>>> 开始获取大学信息:{}", JSON.toJSONString(userPO));
        if (Objects.isNull(userPO)) {
            log.error(":>>> 获取大学信息错误:参数错误");
            throw new BusinessException("获取大学信息错误:参数错误");
        }
        if (StringUtils.isNotBlank(userPO.getUniversityId())) {
            return get(userPO.getUniversityId());
        }
        Query query = new Query().addCriteria(Criteria.where("userId").is(userPO));
        UserParticipateActivityInfo p = mongoTemplate.findOne(query, UserParticipateActivityInfo.class);
        if (Objects.isNull(p)) {
            return null;
        }
        if (StringUtils.isBlank(p.getUniversityId())) {
            return null;
        }
        return get(p.getUniversityId());
    }

    @Override
    public UniversityInfo get(String id) {
        Query query = new Query().addCriteria(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, UniversityInfo.class);
    }

    @Override
    public UniversityInfo saveOrUpdate(UniversityInfo saveOrUpdateUniversityInfo) {
        Query query = new Query();
        String name = saveOrUpdateUniversityInfo.getName();
        query.addCriteria(Criteria.where("name").is(name));
        UniversityInfo universityInfo = this.mongoTemplate.findOne(query, UniversityInfo.class);
        if(Objects.nonNull(universityInfo)){
            return universityInfo;
        }
        Update update = new Update();
        update.setOnInsert("deleted",false);
        update.set("value", saveOrUpdateUniversityInfo.getName());
        update.set("dataType", saveOrUpdateUniversityInfo.getDataType());
        Set<String> names = IkAnalyzerUtil.getSegText(name,true);
        update.set("nameKeywords",names);
        update.set("nameKeys",IkAnalyzerUtil.getSegTextStr(names));
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.returnNew(Boolean.TRUE);
        findAndModifyOptions.upsert(Boolean.TRUE);
        return this.mongoTemplate.findAndModify(query, update, findAndModifyOptions, UniversityInfo.class);
    }

    @Override
    public HashMap<String, UniversityInfo> getUniversitys(List<String> ids) {
        Query query = new Query().addCriteria(Criteria.where("id").in(ids));
        List<UniversityInfo> datas = mongoTemplate.find(query, UniversityInfo.class);
        if (CollectionUtils.isEmpty(datas)) {
            return new HashMap<>();
        }
        HashMap<String, UniversityInfo> res = new HashMap<>();
        for (UniversityInfo u : datas) {
            res.put(u.getId(), u);
        }
        return res;
    }
}
